﻿using Black.Surface2DEngine.Graphic2D;
using Black.Surface2DEngine.Graphic2D.Render2D;
using Black.Surface2DEngine.Input.Surface;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Black.Surface2DEngine.Test.Screens
{
    /// <summary>
    /// Ecran principal
    /// </summary>
    public class MasterScreen: Screen
    {
        #region Variables d'instances

        private Entity2DTexture m_oTouchZone1;
        private Entity2DTexture m_oTouchZone2;

        #endregion

        #region Fonctions protégées

        /// <summary>
        /// Charge les ressources
        /// </summary>
        protected override void LoadContent()
        {
            // Chargement des textures

            this.LoadRessource<Texture2D>("Wallpaper02");
            this.LoadRessource<Texture2D>("Square");
            this.LoadRessource<SpriteFont>("MasterFont");
        }

        /// <summary>
        /// Crée les objets logique
        /// </summary>
        protected override void LoadLogic()
        {
            Entity2DTexture oEntity;
            Entity2DText oText;

            // Création du fond

            oEntity = this.Entity2DCreate<Entity2DTexture>();
            oEntity.Texture = this.GetRessource<Texture2D>("Wallpaper02");

            // Création des objets de touch

            this.m_oTouchZone1 = this.Entity2DCreate<Entity2DTexture>(this.Graphic.ScreenCenterX - (this.Graphic.ScreenWidth / 4.0f), this.Graphic.ScreenCenterY);
            this.m_oTouchZone1.Texture = this.GetRessource<Texture2D>("Square");
            this.m_oTouchZone1.SetAlign(Entity2DAlignHoriz.Center, Entity2DAlignVert.Center);

            this.m_oTouchZone2 = this.Entity2DCreate<Entity2DTexture>(this.Graphic.ScreenCenterX + (this.Graphic.ScreenWidth / 4.0f), this.Graphic.ScreenCenterY);
            this.m_oTouchZone2.Texture = this.GetRessource<Texture2D>("Square");
            this.m_oTouchZone2.SetAlign(Entity2DAlignHoriz.Center, Entity2DAlignVert.Center);

            // Création des textes

            oText = this.Entity2DCreate<Entity2DText>(this.Graphic.ScreenCenterX - (this.Graphic.ScreenWidth / 4.0f), this.Graphic.ScreenCenterY);
            oText.SetAlign(Entity2DAlignHoriz.Center, Entity2DAlignVert.Center);
            oText.Text = "Sample Process";
            oText.Font = this.GetRessource<SpriteFont>("MasterFont");
            oText.Color = Color.Black;

            oText = this.Entity2DCreate<Entity2DText>(this.Graphic.ScreenCenterX + (this.Graphic.ScreenWidth / 4.0f), this.Graphic.ScreenCenterY);
            oText.SetAlign(Entity2DAlignHoriz.Center, Entity2DAlignVert.Center);
            oText.Text = "Sample Entities";
            oText.Font = this.GetRessource<SpriteFont>("MasterFont");
            oText.Color = Color.Black;
        }

        /// <summary>
        /// Met à jour la logique de traitement de l'écran
        /// </summary>
        /// <param name="p_fTime">Indicateur temporel</param>
        protected override void UpdateOpen(float p_fTime)
        {
            // Recherche d'un contact sur une des zones

            foreach (TouchContact oContact in TouchManager._ContactsNew)
            {
                Rectangle oRect = new Rectangle((int)oContact.Contact.X, (int)oContact.Contact.Y, 2, 2);

                if (this.m_oTouchZone1.Zone.Contains(oRect))
                {
                    this.Graphic.CreateScreen<ProcessScreen>(true);
                    this.Close();
                }
                else if (this.m_oTouchZone2.Zone.Contains(oRect))
                {
                    this.Graphic.CreateScreen<EntityScreen>(true);
                    this.Close();
                }
            }
        }

        #endregion
    }
}
